package com.saleSystem.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.saleSystem.common.ServerResponse;
import com.saleSystem.pojo.Content;
import com.saleSystem.pojo.User;
import com.saleSystem.service.IProductService;
import com.saleSystem.service.IUserService;
import com.saleSystem.util.FileUpload;
import com.saleSystem.vo.Product;


/**
 * 产品管理控制
 * @author guanwei
 *
 */
@Controller("productManageController")
@RequestMapping("/")
public class ProductManageController {

	private static final Logger logger = LoggerFactory.getLogger(ProductManageController.class);
	
	@Autowired
	private IProductService iProductService;
	
	@Autowired
	private IUserService iUserService;
	
	
	/**
	 * 发布页面
	 * @return
	 */
	@RequestMapping(value="public",method= RequestMethod.GET)
	public String publicContent(ModelMap map,HttpSession session){
/*
		String username = (String)session.getAttribute("userName");
		User user = iUserService.getUser(username);
*/

		User user = (User)session.getAttribute("user");
		map.addAttribute("user",user);
		return "public";
	}
	
	/**
	 * 发布产品
	 * @param map
	 * @param title
	 * @param summary
	 * @param image
	 * @param detail
	 * @param price
	 * @return
	 */
	@RequestMapping(value="publicSubmit",method= RequestMethod.POST)
	public String publicSubmitContent(ModelMap map,@RequestParam String title,@RequestParam String summary,@RequestParam String image,
			@RequestParam String detail,@RequestParam Long price,HttpSession session) {
		
		User user = (User)session.getAttribute("user");

		Content content = new Content();
		content.setDetail(detail);
		content.setImage(image);
		content.setPrice(price);
		content.setSummary(summary);
		content.setTitle(title);
		Content newContent = iProductService.insertContent(content);
		Product product = ContentToProduct(newContent);
		map.addAttribute("product", product);
		map.addAttribute("user",user);
		logger.info("发布新产品!");
		
		return "publicSubmit";
	}
	
	private Product ContentToProduct(Content content){
		Product product = new Product();
		if(content != null){
			product.setDetail(content.getDetail());
			product.setId(content.getId());
			product.setImage(content.getImage());
			product.setPrice(content.getPrice());
			product.setSummary(content.getSummary());
			product.setTitle(content.getTitle());
		}
		return product;
	}
	
	/**
	 * 编辑页面
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="edit",method= RequestMethod.GET)
	public String edit(@RequestParam(value = "id")Integer id,ModelMap map,HttpSession session){

		User user = (User)session.getAttribute("user");
		Content content = iProductService.editProduct(id);
		Product product = ContentToProduct(content);
		map.addAttribute("product", product);
		map.addAttribute("user",user);
		return "edit";
	}
	
	/**
	 * 编辑产品
	 * @param id
	 * @param title
	 * @param summary
	 * @param image
	 * @param detail
	 * @param price
	 * @param map
	 * @return
	 */
	@RequestMapping(value="editSubmit",method= RequestMethod.POST)
	public String editSubmit(@RequestParam(value = "id")Integer id,@RequestParam String title,@RequestParam String summary,@RequestParam String image,
			@RequestParam String detail,@RequestParam Long price,ModelMap map,HttpSession session){
		
		User user = (User)session.getAttribute("user");
		logger.info("编辑产品，产品ID =" + id);
		Content content = new Content();
		content.setId(id);
		content.setDetail(detail);
		content.setImage(image);
		content.setPrice(price);
		content.setSummary(summary);
		content.setTitle(title);
		Content newContent = iProductService.updateContent(content);
		Product product = ContentToProduct(newContent);
		map.addAttribute("product", product);
		map.addAttribute("user",user);
		return "editSubmit";
	}
	
	
	/**
	 * 删除产品
	 * @param id
	 * @return
	 */
	@RequestMapping(value="api/delete",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse<Boolean> delete(@RequestParam("id")Integer id){
		
		ServerResponse<Boolean> response = iProductService.deleteProduct(id);
		logger.info("删除产品，产品id =" + id);
        return response;
    }
	
	
	
	@RequestMapping(value="api/upload",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse<String> uploadPic(MultipartFile file, HttpServletRequest request){
		
			ServerResponse<String> response= FileUpload.uploadFile(file,request);
            return response;
    }
	
	
}
